<?php
class PostModel extends Model {

    /**
     * status: draft,pending,private,publish  草稿、待审、发布(private暂时不考虑)
     *
     */
    protected $fields=array(
        'id','model','title','created','modified','userid','auditid','author','source','keywords','summary','thumb','jump','status','viewnum',
        'viewpermit','comment','commentnum','content',
        '_pk'=>'id',
        '_autoInc'=>true
    );

    private $popedomCategoriesArr;     //授权栏目ID数组

    protected $_auto=array(
        array('title','trim','ALL','function'),
    );

    protected $_validate=array(
        array('title','require','标题不能为空')
    );


    /**
     * 检查文档是否审核通过
     * @param $postid 文档ID
     * @return boolean 通过与否
     */
    public function checkAudit($postid){
        $auditid = $this->where("id={$postid}")->getField('auditid');
        return !empty($auditid);
    }

    public function auditPost($postid,$auditid){
        return $this->where("id={$postid}")->save(array('auditid'=>$auditid));
    }

    /**
     * 更新文档
     * @Event:UpdatePost (TODO)
     */
    public function updatePost(){
        $baseVo=$this->create();
        if(!$baseVo) {
            return false;
        }
        //TODO 未检查是否有权限修改此篇POST
        $postModel=$_POST['postModel'];
        $model=null;
        //元数据处理
        $postMetaDao=new PostMetaModel();
        $baseVo=array_merge($baseVo, $postMetaDao->beforeSave($baseVo));
        //更新时间
        $baseVo['modified']=date('Y-m-d H:i:s');
        $this->startTrans();
        if($this->save($baseVo,'id='.$baseVo['id'])){
            $postMetaDao->afterSave($baseVo);
            if($this->commit()) {
                return true;
            }
        }
        $this->rollback();
        $this->error=UPDATE_FAIL;
        return false;
     }

}
